package com.rocketmq.temp3.common.components;

import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;

/**
 * 手动事务控制
 */
@Component
public class TransactionUtils {

    private static final DataSourceTransactionManager transactionManager;

    static {
        transactionManager = SpringContextUtils.getBean(DataSourceTransactionManager.class);
    }

    public static TransactionStatus begin() {
        return transactionManager.getTransaction(new DefaultTransactionAttribute());
    }

    public static void commit(TransactionStatus transactionStatus) {
        transactionManager.commit(transactionStatus);
    }

    public static void rollback(TransactionStatus transactionStatus) {
        transactionManager.rollback(transactionStatus);
    }

}
